package com.sxf.util;

import java.io.IOException;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;

import org.sitemesh.DecoratorSelector;
import org.sitemesh.builder.SiteMeshFilterBuilder;
import org.sitemesh.config.ConfigurableSiteMeshFilter;
import org.sitemesh.content.Content;
import org.sitemesh.webapp.WebAppContext;

/**
 * 不再使用sitemesh.xml 配置文件，改为html中的meta来配置
 * 
 * @author SXF
 * 
 */
public class MetaSiteMeshFilter extends ConfigurableSiteMeshFilter {
	private final String DECORATOR_PREFIX = "/view/jsp/blog/template/";
	private final String DECORATOR_SUFFIX = ".jsp";

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		super.init(filterConfig);
	}

	@Override
	protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
		super.applyCustomConfiguration(builder);
		builder.setCustomDecoratorSelector(new DecoratorSelector<WebAppContext>() {

			@Override
			public String[] selectDecoratorPaths(Content content,
					WebAppContext context) throws IOException {
				String decoratorPath = content.getExtractedProperties()
						.getChild("meta").getChild("layout").getValue();
				if (decoratorPath == null || decoratorPath.trim().equals("")) {
					return new String[] {};
				} else {
					return new String[] { DECORATOR_PREFIX
							+ decoratorPath.trim() + DECORATOR_SUFFIX };
				}
			}
		});
	}
}
